﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace MazeNetGUI
{
    public class CardPanel : Panel
    {
        /// <summary>
        /// Aktuelle  Karte, die das Panel anzeigt
        /// </summary>
        public cardType Card
        {
            get { return this._mCard; }
            set
            {
                _mCard = value;
                this.Refresh();
            }
        }
        private cardType _mCard;

        private static readonly SolidBrush WallBrush;
        private static readonly SolidBrush WayBrush;
        private static readonly SolidBrush PlayerBrush;
        private static readonly SolidBrush ReachableBrush;
        private static Font PlayerFont;

        static CardPanel()
        {
            WallBrush = new SolidBrush(Color.Brown);
            WayBrush = new SolidBrush(Color.Green);
            PlayerBrush = new SolidBrush(Color.White);
            PlayerFont = new Font("Arial", 10, FontStyle.Regular);
            ReachableBrush = new SolidBrush(Color.FromArgb(60, Color.Lime));
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            int _lWayWidth = this.Width / 3;
            StringBuilder _lPlayers;

            if (_mCard != null)
            {
                //Wand malen
                e.Graphics.FillRectangle(WallBrush, e.Graphics.ClipBounds);
                //Gang malen
                if (_mCard.openings.top)
                    e.Graphics.FillRectangle(WayBrush, new Rectangle(e.ClipRectangle.X + _lWayWidth, e.ClipRectangle.Y,  _lWayWidth, 2 * _lWayWidth));
                if (_mCard.openings.bottom)
                    e.Graphics.FillRectangle(WayBrush, new Rectangle(e.ClipRectangle.X + _lWayWidth, e.ClipRectangle.Y + _lWayWidth, _lWayWidth, 2 * _lWayWidth));
                if (_mCard.openings.left)
                    e.Graphics.FillRectangle(WayBrush, new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y + _lWayWidth,  2 * _lWayWidth, _lWayWidth));
                if (_mCard.openings.right)
                    e.Graphics.FillRectangle(WayBrush, new Rectangle(e.ClipRectangle.X + _lWayWidth, e.ClipRectangle.Y + _lWayWidth, 2 * _lWayWidth, _lWayWidth));

                //Spieler malen
                _lPlayers = new StringBuilder();
                _lPlayers.Append('(');
                foreach (int player in _mCard.pin)
                {
                    _lPlayers.Append(player).Append(',');
                }
                _lPlayers.Append(')');
                e.Graphics.DrawString(_lPlayers.ToString(), PlayerFont, PlayerBrush, e.ClipRectangle.Location);

                //Schatz malen
                if (_mCard.treasureSpecified)
                {
                    e.Graphics.DrawString(_mCard.treasure.ToString(), PlayerFont, PlayerBrush,e.ClipRectangle.Left, e.ClipRectangle.Top + 12);
                }
            }
        }

        protected override void OnResize(EventArgs eventargs)
        {
            base.OnResize(eventargs);
            this.Refresh();
        }

        public void SetReachable()
        {
            Graphics g = this.CreateGraphics();
            g.FillRectangle(ReachableBrush, new Rectangle(0, 0, this.Width, this.Height));
        }
    }
}
